package prototypes;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ConnectionHandler implements Runnable {

	private Socket socket;

	public ConnectionHandler(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		long threadID = Thread.currentThread().getId();
		System.out.println(threadID + ": Handling connection using port "
				+ socket.getLocalPort() + ".");

		DataInputStream input;
		try {
			input = new DataInputStream(socket.getInputStream());

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));

			String currentLine = null;

			while ((currentLine = reader.readLine()) != null
					&& !currentLine.equals("")) {
				System.out.println(threadID + ":\t" + currentLine);
			}

			System.out.println("============================================");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
